package com.chenlei.cltoppopup;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;

import com.chenlei.toppopup.CLTopPopup;
import com.chenlei.toppopup.model.CLModel;
import com.chenlei.toppopup.util.PopupWindowUtils;

import java.util.ArrayList;
import java.util.Random;

public class MainActivity extends AppCompatActivity
{
  private Button testButton;
  private CLTopPopup clTopPopup;

  @Override
  protected void onCreate(Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    testButton = (Button) findViewById(R.id.testbutton);
    testButton.setOnClickListener(new View.OnClickListener()
    {
      @Override
      public void onClick(View v)
      {
        CLTopPopup.TextBuilder left = new CLTopPopup.TextBuilder()
            .setHeight(50)
            .setShowLine(false)
            .setNormalBgColor(android.R.color.white)
            .setSelectBgColor(R.color.colorAccent)
            .setNormalTextColor(android.R.color.black)
            .setSelectTextColor(android.R.color.white)
            .setTextSize(20);

        final CLTopPopup.TextBuilder right = new CLTopPopup.TextBuilder()
            .setHeight(44)
            .setShowLine(true)
            .setNormalDrawable(R.mipmap.ic_launcher)
            .setSelectDrawable(R.mipmap.ic_launcher_round)
            .setNormalTextColor(android.R.color.black)
            .setSelectTextColor(R.color.colorAccent)
            .setNormalBgColor(android.R.color.white)
            .setSelectBgColor(android.R.color.black)
            .setTextSize(16)
            .setLineMarginStart(16)
            .setLineMarginEnd(16)
            .setLineColor(R.color.colorAccent)
            .setTextLeft(false);

        CLTopPopup.BottomViewBuilder bottomViewBuilder = new CLTopPopup.BottomViewBuilder()
            .setLeftText("取消").setRightText("确定");

        CLTopPopup.LeftEndTextBuilder endTextBuilder = new CLTopPopup.LeftEndTextBuilder()
            .setText("添加").setTextColor(R.color.colorPrimary).setTextSize(16).setHeight(30);
        CLTopPopup.Builder builder = new CLTopPopup.Builder()
            .setHeightPer(0.5f)
            .setType(CLTopPopup.TOPPOPUP_TWO)
            .setRightPer(0.7f).setLeftTextBuilder(left)
            .setRightTextBuilder(right)
            .setLeftEndTextBuilder(endTextBuilder)
            .setShowBottomView(true)
            .setBottomViewBuilder(bottomViewBuilder)
            .setItemClickListener(new CLTopPopup.OnItemClickListener()
            {
              @Override
              public void onLeftItemClick(int pos)
              {
                Log.d("23", "点击了左侧的第" + pos + "个item");

                ArrayList<CLModel> list = getTestData();
                if (pos == 2)
                {
                  CLModel clModel = new CLModel();
                  clModel.setText("测试 输入");
                  clModel.setLeft(true);
                  clModel.setRightDrawable(R.drawable.icon_pen);
                  clModel.setRightSelectDrawable(R.drawable.icon_pen);
                  clModel.setEditText(true);
                  list.add(clModel);
                }
                clTopPopup.setRightData(list, 0);
              }

              @Override
              public void onRightItemClick(int leftPos, int rightPos)
              {
                Log.d("123", "选择了左侧第" + leftPos + "个" + ",右侧第" + rightPos + "个");
              }

              @Override
              public void onLeftEndItemClick()
              {
                Log.d("123", "点击了自定义添加");
              }

              @Override
              public void onBottomLeftItemClick()
              {

              }

              @Override
              public void onBottomRightItemClick()
              {

              }
            }).setOnDismissListener(new CLTopPopup.OnDismissListener()
            {
              @Override
              public void onTopPopupDismiss()
              {
                PopupWindowUtils.updatePopupWindowBgAlpha(MainActivity.this, 0.7f);
              }
            });

        //        CLTopPopup.Builder builder = new CLTopPopup.Builder()
        //                .setType(CLTopPopup.TOPPOPUP_ONE)
        //                .setRightTextBuilder(right)
        //                .setShowBottomView(true)
        //                .setBottomViewBuilder(bottomViewBuilder)
        //                .setItemClickListener(new CLTopPopup.OnItemClickListener()
        //                {
        //                  @Override
        //                  public void onLeftItemClick(int pos)
        //                  {
        //                    Log.d("23", "点击了左侧的第" + pos + "个item");
        //                    ArrayList<CLModel> list = getTestData();
        //                    CLModel clModel = new CLModel();
        //                    clModel.setText("测试 输入");
        //                    clModel.setLeft(true);
        //                    clModel.setRightDrawable(R.drawable.icon_pen);
        //                    clModel.setRightSelectDrawable(R.drawable.icon_pen);
        //                    clModel.setEditText(true);
        //                    list.add(clModel);
        //                    clTopPopup.setRightData(list, list.size() - 1);
        //                  }
        //
        //                  @Override
        //                  public void onRightItemClick(int leftPos, int rightPos)
        //                  {
        //                    Log.d("123", "选择了左侧第" + leftPos + "个" + ",右侧第" + rightPos + "个");
        //                  }
        //
        //                  @Override
        //                  public void onLeftEndItemClick()
        //                  {
        //                    Log.d("123", "点击了自定义添加");
        //                  }
        //
        //                  @Override
        //                  public void onBottomLeftItemClick() {
        //                    Log.d("123", "左侧下部按钮");
        //                  }
        //
        //                  @Override
        //                  public void onBottomRightItemClick() {
        //                    Log.d("123", "右侧下部按钮 " + clTopPopup.getInputText());
        //                    clTopPopup.dismiss();
        //                  }
        //                }).setOnDismissListener(new CLTopPopup.OnDismissListener() {
        //                  @Override
        //                  public void onTopPopupDismiss() {
        //                    PopupWindowUtils.updatePopupWindowBgAlpha(MainActivity.this,1.0f);
        //                  }
        //                });

        clTopPopup = new CLTopPopup(getApplicationContext(), testButton, builder);
        clTopPopup.setLeftData(getTestData(), 0);
        //        clTopPopup.setRightData(getTestData(), 0);
        PopupWindowUtils.updatePopupWindowBgAlpha(MainActivity.this, 0.7f);
        clTopPopup.show();
      }
    });
  }

  private ArrayList<CLModel> getTestData()
  {
    Random random = new Random();
    int max = random.nextInt(10) + 1;
    ArrayList<CLModel> arrayList = new ArrayList<>();
    for (int i = 0; i < max; i++)
    {
      CLModel clModel = new CLModel();
      clModel.setText("测试--" + (i + 1));
      arrayList.add(clModel);
    }
    return arrayList;
  }

}


